# Copyright (C) 2006  Apptility LLC  http://www.apptility.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

# Odotcron
module Odotcron
 $ODOTCRON_LOGFILE_NAME = "odot_cron_debug.log"
 #---adding RailsCrons to the db----
 #these adding of railsCrons should typically only be run once - unless it is a cron that ends of course

  def Odotcron.create_test_cron
    AjubyLogger.logInfo  "Starting Ajuby Toolkit Cron.."
    logInfo("Starting Ajuby Toolkit Cron..")
    
    RailsCron.create(
      :command => "Odotcron.testDebug",
      :start => 1.minutes.from_now,
      :every => 1.minutes,
      :finish => 1.years.from_now
    )
  end

  def Odotcron.create_feed_aggregator_cron
    AjubyLogger.logInfo  "Starting Ajuby Toolkit Cron: Feed Aggregator"
    logInfo("Starting Ajuby Toolkit Cron: Feed Aggregator")
    
    RailsCron.create(
      :command => "Odotcron.feed_aggregator_start",
      :start => 1.minutes.from_now,
      :every => 6.hours,
      :finish => 1.years.from_now
    )
  end

  def Odotcron.create_feed_delivery_daily_cron
    AjubyLogger.logInfo  "Starting Ajuby Toolkit Cron: Feed Delivery Daily Cron"
    logInfo("Starting Ajuby Toolkit Cron: Feed Delivery Daily Cron")
    
    RailsCron.create(
      :command => "Odotcron.feed_deliver_daily",
      :start => 1.minutes.from_now,
      :every => 11.hours,
      :finish => 1.years.from_now
    )
  end
  
  #-----------  "BACKUP CRON CODING START" ----------------
  
  def Odotcron.create_backup_delivery_daily_cron
    AjubyLogger.logInfo  "Starting Ajuby Toolkit Cron: Backup Delivery Daily Cron"
    logInfo("Starting Ajuby Toolkit Cron: Backup Delivery Daily Cron")
    
    RailsCron.create(
      :command => "Odotcron.backup_deliver_daily",
      :start => 1.minutes.from_now,
      :every => 11.hours,
      :finish => 1.years.from_now
    )
  end
  
   def Odotcron.create_backup_delivery_weekly_cron
    AjubyLogger.logInfo  "Starting Ajuby Toolkit Cron: Backup Delivery Weekly Cron"
    logInfo("Starting Ajuby Toolkit Cron: Backup Delivery Weekly Cron")
    
    RailsCron.create(
      :command => "Odotcron.backup_deliver_weekly",
      :start => 1.minutes.from_now,
      :every => 6.days,
      :finish => 1.years.from_now
    )
  end
  
   def Odotcron.create_backup_delivery_hourly_cron
    AjubyLogger.logInfo  "Starting Ajuby Toolkit Cron: Backup Delivery Hourly Cron"
    logInfo("Starting Ajuby Toolkit Cron: Backup Delivery Hourly Cron")
    
    RailsCron.create(
      :command => "Odotcron.backup_deliver_hourly",
      :start => 1.minutes.from_now,
      :every => 1.hours,
      :finish => 1.years.from_now
    )
  end
  
  def Odotcron.create_backup_destroy_cron
    AjubyLogger.logInfo  "Destroy Ajuby Toolkit Cron"
    logInfo(" Backup Destroy")
    
    RailsCron.destroy_all
  
  end
  
  def Odotcron.backup_deliver_daily
     logInfo("#{Time.now}\tRunning Backup Delivery Daily Cron Debug, :Status: Working")     
     #DbbackupController.deliver_schedule
     DbbackupDeliveryUtil.deliver_schedule
  end
  
   def Odotcron.backup_deliver_weekly
     logInfo("#{Time.now}\tRunning Backup Delivery Weekly Cron Debug, :Status: Working")     
     #DbbackupController.deliver_schedule
     DbbackupDeliveryUtil.deliver_schedule
  end
  
   def Odotcron.backup_deliver_hourly
     logInfo("#{Time.now}\tRunning Backup Delivery Hourly Cron Debug, :Status: Working")     
     #DbbackupController.deliver_schedule
     DbbackupDeliveryUtil.deliver_schedule
  end
  
  # -------------------- " BACKUP CRON CODING FINISHED " ----------------
  
  # Feed Aggregator Cron Start point
  def Odotcron.feed_aggregator_start
	 logInfo("#{Time.now}\tRunning Feed Aggregator Cron Debug, :Status: Working")
	 FeedAggregator.aggregate_and_deliver
  end
  
  # Feed Aggregator Cron Start point
  def Odotcron.feed_deliver_daily
	 logInfo("#{Time.now}\tRunning Feed Delivery Daily Cron Debug, :Status: Working")
	 FeedDeliveryUtil.deliver_daily
  end 

  
  #---Test the cron debug method-----------------
  def Odotcron.testDebug
	 logInfo("#{Time.now}\tRunning Test Cron Debug, Testing Rails Cron.:Status: Working")
  end
  
  
  # Logging method
  def self.logInfo(*args)
	    File.open(File.join(File.expand_path(RAILS_ROOT), "log", $ODOTCRON_LOGFILE_NAME), "a") do |f| 
	      f.puts  "#{Time.now}\t#{args.join "\t"}" 
      end
  end
  
end